﻿using Syspetro.Core.Extensions;
using System;
using System.Collections.Generic;

namespace Syspetro.Core.AppDeal
{
    [Serializable]
    public class SpActionResult<T>
    {
        public string Msg { get; set; } = "操作成功";
        public T Data { get; set; }
        public ResultCode Code { get; set; } = ResultCode.Succeed;
        private bool success = true;
        public bool Success
        {
            get
            {
                return success;
            }
            set
            {
                if (!value) Code = ResultCode.Fail;
                success = value;
            }
        }
        public SpActionResult()
        {
        }
        public SpActionResult(T data) : base()
        {
            Data = data;
        }
        public SpActionResult(T data, string msg)
        {
            Data = data;
            Msg = msg;
        }
        public SpActionResult(T data, string msg, ResultCode code)
        {
            Data = data;
            Msg = msg;
            Code = code;
        }
    }
    [Serializable]
    public class SpActionResult : SpActionResult<List<SpActionResult<string>>>
    {
        List<string> TypeList { get; set; }
        public SpActionResult()
        {

        }
        public SpActionResult(SpActionResult<string> data)
        {
            Data = new List<SpActionResult<string>> { data };
            TypeList = new List<string> { data.GetType().Name };
        }
        public void Add(SpActionResult<string> data)
        {
            if (Data == null)
            {
                Data = new List<SpActionResult<string>>();
                TypeList = new List<string>();
            }
            TypeList.Add(data.GetType().Name);
            Data.Add(data);
        }

        public SpActionResult<string> Get()
        {
            if(Data.Count == 1)
            {
                return new SpActionResult<string> { Success = Data[0].Success, Msg = Data[0].Msg, Data = Data[0].Data, Code = Data[0].Code };
            }
            else
            {
                List<string> datas = new();
                bool bl = true;
                string msg = "";
                ResultCode code = ResultCode.Succeed;
                foreach(var data in Data)
                {
                    datas.Add(data.Data);
                    bl = data.Success;
                    if (!bl)
                    {
                        msg = data.Msg;
                    }
                }
                if (bl) return new SpActionResult<string>(datas.ToJson());
                return new SpActionResult<string> { Success = false, Code = code, Msg = msg, Data = datas.ToJson() };
            }
        }
    }
}
